package com.hmall.order.web;

import com.hmall.common.client.ItemClient;
import com.hmall.common.client.Userclient;
import com.hmall.common.dto.Address;
import com.hmall.common.dto.Item;
import com.hmall.order.Interceptor.UserContext;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.pojo.OrderLogistics;
import com.hmall.order.pojo.RequestParams;
import com.hmall.order.service.IOrderDetailService;
import com.hmall.order.service.IOrderService;
import com.hmall.order.service.impl.OrderLogisticsService;
import com.hmall.order.util.SnowflakeIdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/order")
public class OrderController {

   @Autowired
   private IOrderService orderService;

   @Autowired
   private IOrderDetailService orderDetailService;

   @Autowired
   private OrderLogisticsService orderLogisticsService;

   @Autowired
   private ItemClient client;

   @Autowired
   private Userclient userclient;
    @Autowired
    private OrderMapper orderMapper;

   @GetMapping("/{id}")
   public Order queryOrderById(@PathVariable("id") Long orderId) {
      return orderService.getById(orderId);
   }

   @Transactional
   @PostMapping
   public Long saveOrder(@RequestBody RequestParams params) {
      Order order = new Order();
      // 根据雪花算法生成订单id
//      SnowflakeIdGenerator idGenerator = new SnowflakeIdGenerator(1); // 假设机器id为1
//      long orderId = idGenerator.generateId();
//      long orderId = 1864168461L;//写死的
//      order.setId(orderId);


      //根据itemId查询商品信息
      Item item = client.getItemById(params.getItemId());
      // 设置订单信息
      order.setTotalFee(item.getPrice()*params.getNum());
      order.setPaymentType(params.getPaymentType());
      String userId = UserContext.getUserId();
      order.setUserId(Long.valueOf(userId));
      order.setStatus(1);
      orderMapper.insert(order);
//      orderService.save(order);


      // 保存订单详情信息
      OrderDetail orderDetail = new OrderDetail();
      orderDetail.setOrderId(order.getId());
      orderDetail.setItemId(params.getItemId());
      orderDetail.setNum(params.getNum());
      orderDetail.setName(item.getName());
      orderDetail.setPrice(item.getPrice());
      orderDetail.setSpec(item.getSpec());
      orderDetail.setImage(item.getImage());
      orderDetailService.save(orderDetail);

      // 保存物流信息
      Address address = userclient.findAddressById(params.getAddressId());
      OrderLogistics orderLogistics = new OrderLogistics();
      orderLogistics.setOrderId(order.getId());
      orderLogistics.setContact(address.getContact());
      orderLogistics.setMobile(address.getMobile());
      orderLogistics.setProvince(address.getProvince());
      orderLogistics.setCity(address.getCity());
      orderLogistics.setTown(address.getTown());
      orderLogistics.setStreet(address.getStreet());
      orderLogisticsService.save(orderLogistics);

      // 减库存
      client.updateStock(params.getItemId(),params.getNum());


      return order.getId();
   }
}
